import 'dart:async';
import 'dart:math';

import 'package:sensors_plus/sensors_plus.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_log.dart';
/*
传感器
https://pub-web.flutter-io.cn/packages/sensors_plus/install

dependencies:
  sensors_plus: ^6.1.2

import 'package:sensors_plus/sensors_plus.dart';

 */
abstract class XSensorUtils{


  //获取加速度计数据

// 监听加速度计数据
  void listenToAccelerometer() {


    accelerometerEventStream()
        .listen((AccelerometerEvent event) {
      print('Accelerometer: x=${event.x}, y=${event.y}, z=${event.z}');

      //如:设备姿态计算

      // 计算俯仰角 (pitch) 和横滚角 (roll)
      final pitch =
          (180 / 3.14159) * atan2(event.y, sqrt(event.x * event.x + event.z * event.z));

      final roll =
          (180 / 3.14159) * atan2(event.x, sqrt(event.y * event.y + event.z * event.z));

      print('Pitch: ${pitch.toStringAsFixed(2)}°, Roll: ${roll.toStringAsFixed(2)}°');
    });
  }

// 一次性读取加速度数据
  void getAccelerometerOnce() async {
    AccelerometerEvent? event = await accelerometerEventStream().first;
    print('Current acceleration: $event');
  }


  //获取陀螺仪数据
  void listenToGyroscope() {
    gyroscopeEventStream().listen((GyroscopeEvent event) {
      print('Gyroscope: x=${event.x}, y=${event.y}, z=${event.z}');
    });
  }


  //获取磁力计数据
  static void listenToMagnetometer() {
    magnetometerEventStream().listen((MagnetometerEvent event) {
      XLogUtils.printLog('---listenToMagnetometer: x=${event.x}, y=${event.y}, z=${event.z}');
    });
  }



  static const double shakeThreshold = 2.7;
  static const int shakeSlopTime = 500;
  static const int shakeCountResetTime = 3000;
  DateTime _lastShakeTime = DateTime.now();
  int _shakeCount = 0;


  static const double stepThreshold = 1.5;
  DateTime _lastStepTime = DateTime.now();
  int _stepCount = 0;
  double _lastAcceleration = 0;

  //获取用户加速度
  void listenToUserAccelerometer() {
    userAccelerometerEventStream().listen((UserAccelerometerEvent event) {
      print('User Acceleration: x=${event.x}, y=${event.y}, z=${event.z}');

      //如:设备摇晃检测
      final double acceleration = (event.x.abs() + event.y.abs() + event.z.abs());
      final now1 = DateTime.now();
      if (acceleration > shakeThreshold) {
        if (now1.difference(_lastShakeTime).inMilliseconds > shakeSlopTime) {
          _shakeCount++;
          _lastShakeTime = now1;

          if (_shakeCount >= 3) {

            _shakeCount = 0;

            //_onShakeDetected();
            print('Shake detected!');
            // 执行摇晃后的操作，如刷新数据、撤销等

          }
        }
      }
      // 重置计数器
      if (now1.difference(_lastShakeTime).inMilliseconds > shakeCountResetTime) {
        _shakeCount = 0;
      }





      //如:计步器
      final double acceleration1 = sqrt(event.x * event.x + event.y * event.y + event.z * event.z);

      final now = DateTime.now();
      final timeDiff = now.difference(_lastStepTime).inMilliseconds;

      // 检测步伐模式
      if (acceleration1 > stepThreshold &&
          _lastAcceleration <= stepThreshold &&
          timeDiff > 300) { // 防止连续检测

        _stepCount++;
        _lastStepTime = now;
        print('Steps: $_stepCount');
      }

      _lastAcceleration = acceleration1;

    });
  }



}
